package com.xjm.ips.controller.system.basic;

import com.xjm.ips.entity.AjaxJson;
import com.xjm.ips.entity.Department;
import com.xjm.ips.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @GetMapping("/")
    public List<Department> getAllDepartments(){
        return departmentService.getAllDepartments();
    }

    @PostMapping("/")
    public AjaxJson addDepartment(@RequestBody Department department){
        departmentService.addDepartment(department);
        if(department.getResult()==1){
            return AjaxJson.ok("添加部门成功!",department);
        }
        return  AjaxJson.error("添加部门失败!");
    }

    @DeleteMapping("/{id}")
    public AjaxJson deleteDepartment(@PathVariable Integer id){
        Department department = new Department();
        department.setId(id);
        departmentService.deleteDepartment(department);
        if(department.getResult() == 0){
            return AjaxJson.error("该部门有子部门，删除失败!");
        }else if(department.getResult() == -1){
            return AjaxJson.error("该部门含有任职人员，无法删除!");
        }else if(department.getResult() == 1){
            return AjaxJson.ok("删除成功!");
        }
        return AjaxJson.error("删除失败!");
    }
}
